using UnityEngine;

namespace Script.StateMachine
{
    public class HitState:IState
    {
        private FSM manager;
        private Parameter _parameter;
        private AnimatorStateInfo _info;
        
        public HitState(FSM manager)
        {
            this.manager = manager;
            _parameter = manager.parameter;
        }

        public void OnEnter()
        {
            manager.animator.Play("Hit");
            _parameter.health -= _parameter.hitDamage*_parameter.critical;
            manager.ShowHitText();                             //显示受击伤害
            manager.rigidbody2D.velocity = new Vector2(-manager.transform.localScale.x*_parameter.hitSpeed, manager.rigidbody2D.velocity.y);//受击时像后移动
        }
        public void OnUpdate()
        {
            _info = manager.animator.GetCurrentAnimatorStateInfo(0);
            if (_info.normalizedTime>0.65)  //这里需要将动画播放进度设置的小一点，因为如果动画播放进度设置为0.95，这时候player已经砍出去了，但只收到了一次攻击
            {
                _parameter.playerTrans = GameObject.FindWithTag("Player").transform;
                manager.TransitionState(StateType.Chase);
            }

            if (_parameter.health<=0)
            {
                manager.TransitionState(StateType.Death);
            }
        }

        public void OnExit()
        {
            _parameter.isHit = false;
        }
    }
}